package zut.wework.service.impl;

import com.tencentcloudapi.wemeet.client.MeetingClient;
import com.tencentcloudapi.wemeet.common.RequestSender;
import com.tencentcloudapi.wemeet.common.exception.WemeetSdkException;
import com.tencentcloudapi.wemeet.common.profile.HttpProfile;
import com.tencentcloudapi.wemeet.models.BaseResponse;
import com.tencentcloudapi.wemeet.models.meeting.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import zut.wework.entity.Result;
import zut.wework.entity.meeting.CreatMeetingModel;
import zut.wework.entity.meeting.InviteModel;
import zut.wework.entity.meeting.QueryMeetingVo;
import zut.wework.service.MeetingService;
import zut.wework.utils.MeetingUtil;

/**
 * @Classname: MeetingServiceImpl
 * @Description: 腾讯会议相关业务实现
 * @Date: 2022/5/8
 * @Created: by ifaxcaeles
 */
@Service
@Slf4j
public class MeetingServiceImpl implements MeetingService {
    public static final MeetingClient MEETING_CLIENT;
//    private static final Gson GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();

    static {
        HttpProfile profile = new HttpProfile();
        // 腾讯会议分配给三方开发应用的 App ID。企业管理员可以登录 腾讯会议官网，单击右上角【用户中心】
        // 在左侧菜单栏中的【企业管理】>【高级】>【restApi】中进行查看。
        profile.setAppId("210984532");
        // 用户子账号或开发的应用 ID，企业管理员可以登录 腾讯会议官网，单击右上角【用户中心】
        // 在左侧菜单栏中的【企业管理】>【高级】>【restApi】中进行查看（如存在 SdkId 则必须填写，早期申请 API 且未分配 SdkId 的客户可不填写）。
        profile.setSdkId("19370190646");
        // 请求域名
        profile.setHost("https://api.meeting.qq.com");
        // 申请的安全凭证密钥对中的 SecretId，传入请求header，对应X-TC-Key
        profile.setSecretId("zvF4VEcdwlcG9bfyPCkF5xQDIYw8LdNj");
        // 申请的安全凭证密钥对中的 Secretkey，用户签名计算
        profile.setSecretKey("rROP64FlucdPp8kHNP1vyxhMNV7eItBsGOoCCxQzkU7nRtNK");
        // 是否开启请求日志，开启后会打印请求和返回的详细日志
        profile.setDebug(true);
        // 设置请求超时时间，单位s
        profile.setReadTimeout(3);
        // 设置获取连接超时时间，单位s
        profile.setConnTimeout(1);
        RequestSender sender = new RequestSender(profile);
        MEETING_CLIENT = new MeetingClient(sender);
    }

    /**
     * @apilink: https://cloud.tencent.com/document/product/1095/42417
     * @apiuri: https://api.meeting.qq.com/v1/meetings
     * @method: POST
     */
    public Result createMeeting(CreatMeetingModel creatMeetingModel) {
        QueryMeetingVo info = null;
        CreateMeetingRequest request = new CreateMeetingRequest();
        request.setStartTime(creatMeetingModel.getStartTime());
        request.setEndTime(creatMeetingModel.getEndTime());
        request.setUserId(creatMeetingModel.getUserId());
        request.setSubject(creatMeetingModel.getSubject());
        request.setInstanceId(creatMeetingModel.getInstanceId());
        request.setType(creatMeetingModel.getType());
        if (creatMeetingModel.getHosts() != null) {
            request.setHosts(creatMeetingModel.getHosts());
        }
        try {
            QueryMeetingDetailResponse response = MEETING_CLIENT.createMeeting(request);
            info = new QueryMeetingVo(response.getMeetingInfoList());
            log.info(String.valueOf(info));
            return Result.success("[创建会议]-成功", info);
        } catch (WemeetSdkException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @apilink: https://cloud.tencent.com/document/product/1095/42422
     * @apiuri: https://api.meeting.qq.com/v1/meetings/{meetingId}/cancel
     * @method: POST
     */
    public Result cancelMeeting(String userId, String meetingId) {
        CancelMeetingRequest request = new CancelMeetingRequest();
        request.setMeetingId(meetingId);
        request.setInstanceId(1);
        request.setUserId(userId);
        request.setReasonCode(1);
        try {
            BaseResponse response = MEETING_CLIENT.cancelMeeting(request);
            log.info("[删除会议]-成功");
            return Result.success("[删除会议]-成功");
        } catch (WemeetSdkException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param userId
     * @param meetingid
     * @return
     * @apilink: https://cloud.tencent.com/document/product/1095/42418
     * @apiuri: https://api.meeting.qq.com/v1/meetings/{meetingId}?userid={userid}&instanceid={instanceid}
     */
    @Override
    public Result queryMeeting(String userId, String meetingid) {
        QueryMeetingVo info = null;
        QueryMeetingByIdRequest request = new QueryMeetingByIdRequest();
        request.setMeetingId(meetingid);
        request.setUserId(userId);
        request.setInstanceId(1);
        try {
            QueryMeetingDetailResponse response = MEETING_CLIENT.queryMeetingById(request);
            info = new QueryMeetingVo(response.getMeetingInfoList());
            log.info(String.valueOf(info));
            return Result.success("[查询会议]-成功", info);
        } catch (WemeetSdkException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 自己整合的 实际为Modify
     *
     * @param inviteModel
     * @return
     */
    @Override
    public Result inviteInvitees(InviteModel inviteModel) {
        QueryMeetingVo info = null;
        ModifyMeetingRequest request = new ModifyMeetingRequest();
        request.setMeetingId(inviteModel.getMeetingId());
        request.setInstanceId(1);
        request.setUserId(inviteModel.getUserId());
        request.setInvitees(MeetingUtil.idsToUserList(inviteModel.getIdList()));
        try {
            QueryMeetingDetailResponse response = MEETING_CLIENT.modifyMeeting(request);
            info = new QueryMeetingVo(response.getMeetingInfoList());
            log.info(String.valueOf(info));
            return Result.success("[邀请成员]-成功", info);
        } catch (WemeetSdkException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 自己整合的 实际为Create+Modify
     *
     * @param inviteModel
     * @return
     */
    @Override
    public Result createAndInvite(InviteModel inviteModel) {
        QueryMeetingVo info = null;
        CreateMeetingRequest request = new CreateMeetingRequest();
        CreatMeetingModel creatMeetingModel = new CreatMeetingModel(inviteModel.getUserId(), inviteModel.getSubject());
        request.setStartTime(creatMeetingModel.getStartTime());
        request.setEndTime(creatMeetingModel.getEndTime());
        request.setUserId(creatMeetingModel.getUserId());
        request.setSubject(creatMeetingModel.getSubject());
        request.setInstanceId(creatMeetingModel.getInstanceId());
        request.setType(creatMeetingModel.getType());
        request.setInvitees(MeetingUtil.idsToUserList(inviteModel.getIdList()));
        if (creatMeetingModel.getHosts() != null) {
            request.setHosts(creatMeetingModel.getHosts());
        }
        try {
            QueryMeetingDetailResponse response = MEETING_CLIENT.createMeeting(request);
            info = new QueryMeetingVo(response.getMeetingInfoList());
            log.info(String.valueOf(info));
            return Result.success("[创建会议]-成功", info);
        } catch (WemeetSdkException e) {
            return Result.error(e.getMessage());
        }
    }
}
